package cn.kgc.springmvc.day03.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author 课工场
 * @date 2024/11/12
 * @description   springmvc对于页面跳转的支持
 *  1.  转发
 *      forward
 *  2. 重定向
 *      redirect
 */
@Controller
@RequestMapping("rf")
public class RFController {


    //  string   视图名   ->    视图解析器  ->   转发   /main.jsp
    @RequestMapping("m1")
    public String  method1(){

        return "main";
    }


    //  forward关键词    声明转发   不在经过视图解析器的解析
    @RequestMapping("m2")
    public String  method2(){

        return "forward:/main.jsp";
    }


    // 使用传统方式实现转发
    @RequestMapping("m3")
    public void   method3(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setAttribute("info","HttpServletRequest request");
        request.getRequestDispatcher("/main.jsp").forward(request,response);
    }


    // 重定向   不会经过视图解析器
    @RequestMapping("m4")
    public String   method4(HttpSession session){
        session.setAttribute("info","session redirect");
        return "redirect:/main.jsp";
    }



    @RequestMapping("m5")
    public void   method5(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();
        session.setAttribute("info","session redirect");
        response.sendRedirect("/mvc/main.jsp");
    }


}
